home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / quicktime / quicktime for java / draggingsprites / src / ds / actions / skewer.java < prev   
Encoding:
Java Source  |  2000-06-23  |  3.3 KB  |  93 lines

  1. /*
  2.  * QuickTime for Java SDK Sample Code
  3.  
  4.    Usage subject to restrictions in SDK License Agreement
  5.  * Copyright: © 1996-1999 Apple Computer, Inc.
  6.  
  7.  */
  8. package ds.actions;
  9.  
  10. import quicktime.app.actions.*;
  11. import quicktime.qd.*;
  12. import quicktime.*;
  13. import quicktime.std.image.Matrix;
  14. import quicktime.app.image.Transformable;
  15. import quicktime.app.display.Drawable;
  16.  
  17. import java.awt.Dimension;
  18. import java.awt.event.MouseEvent;
  19.  
  20. /**
  21.  * A class that provides the implementation of where a dragged object will be skewed
  22.  * The amount of skew is determined by the distance between the original mouse click and the 
  23.  * current mouse-moved location divided by the specified skewFactors.
  24.  */
  25. public class Skewer extends Dragger {
  26. //____________________________ CLASS METHODS
  27.     /**
  28.      * Set some parameters that will create DragActions. 
  29.      * @param modifierKeyMask - if specified will determine which modifier keys must
  30.      * be depressed for the action to be invoked.
  31.      */
  32.     public Skewer (int xSkewFactor, int ySkewFactor, int modifierKeyMask) {
  33.         this (xSkewFactor, ySkewFactor, modifierKeyMask, MouseResponder.kModifiersExactMatch, 0);
  34.     }
  35.  
  36.     /**
  37.      * Set some parameters that will create DragActions. 
  38.      * @param modifierKeyMask - if specified will determine which modifier keys must
  39.      * be depressed for the action to be invoked.
  40.      * @param modifierTestConditions the test conditions under which the modifier mask is tested
  41.      */
  42.     public Skewer (int xSkewFactor, int ySkewFactor, int modifierKeyMask, int modifierTestConditions, int addedEventInvoker) {
  43.         super (modifierKeyMask, modifierTestConditions, addedEventInvoker);
  44.         xSkew = xSkewFactor;
  45.         ySkew = ySkewFactor;
  46.     }    
  47.     
  48. //____________________________ INSTANCE VARIABLES
  49.     private int xOrigin, yOrigin;    
  50.     private int xSkew, ySkew;
  51.     private Matrix originalMat;
  52.  
  53. //____________________________ INSTANCE METHODS
  54.     /**
  55.      * This method is used by the DragController when the mouse is first pressed down on 
  56.      * the draggable object. If you wish to do anything to your draggable object before it
  57.      * is dragged then you should overide this method. The default implementation does nothing.
  58.      * @param event the mouse down event that may begin the drag action
  59.      */
  60.     public void mousePressed (MouseEvent event) {
  61.         try {
  62.             xOrigin = event.getX();
  63.             yOrigin = event.getY();
  64.             originalMat = target.getMatrix();
  65.         } catch (QTException e) {
  66.             throw new QTRuntimeException (e);
  67.         }
  68.     }
  69.  
  70.     /**
  71.      * This method is called by Dragger when an event is received that meets the conditions for 
  72.      * the object to be dragged.
  73.      * <P>
  74.      * This method will allow the user to skew the object by dragging on it.
  75.      * @param event the mouse drag event that triggered the drag action.
  76.      * @param space The drawable object is the space within which the event has occured.
  77.      */
  78.     public void mouseDragged (MouseEvent event) {
  79.         try {
  80.             // protect against a zero divide
  81.             float skewX = (xSkew != 0 ? (event.getX() - xOrigin) / (float)xSkew : 0);
  82.             float skewY = (ySkew != 0 ? (event.getY() - yOrigin) / (float)ySkew : 0);
  83.             Matrix mat = (Matrix)originalMat.clone();
  84.             
  85.             mat.skew (skewX, skewY, mat.getTx(), mat.getTy());
  86.                     
  87.             target.setMatrix (mat);
  88.         } catch (QTException e) {
  89.             throw new QTRuntimeException (e);
  90.         }
  91.     }
  92. }
  93.